/**
 * @ClassName chapter1.KP4_selectsort
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/7 14:24
 */

import java.util.Arrays;

/**
 * 选择排序
 */
public class KP4_selectsort {

    public static void selectSort(int[] array) {
        if (array == null || array.length < 2) {
            return;
        }
        int N = array.length;
        // 0 ~ N-1
        // 1 ~ N-1
        // 2 ~ N-1
        for (int i = 0; i < N; i++) {
            int minValueIndex = i;
            int j;
            for (j = i + 1; j < N; j++) {
                minValueIndex = array[j] < array[minValueIndex] ? j : minValueIndex;
            }
            if (minValueIndex != i)
                swap(array,i,minValueIndex);
        }
    }

    public static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args) {
        int[] array = new int[]{2,4,1,5,17,15,86};
        System.out.println(Arrays.toString(array));
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }
}
